package ds.factcom.repository;

import java.util.Iterator;

import ds.factcom.domain.client.Client;
import ds.factcom.domain.client.Document;
import ds.factcom.exception.NotExistDomainException;

/**
 * 
 * @author Javier De Luca, Maximiliano Herrera, Matias Samblancat
 *
 */
public class ClientRepository extends BaseRepository<Client>{

	private static ClientRepository instance = null;
	
	private ClientRepository(){
	}
	
	public static ClientRepository getInstance(){
		if (instance == null){
			instance = new ClientRepository();
		}
		return instance;
	}

	public Client findByDocument(String documentType, String documentNumber) throws NotExistDomainException {
		boolean found = false;
		Document document = new Document(documentType, documentNumber);
		for (Iterator<Client> iterator = getAllEntities().iterator(); iterator.hasNext();) {
			Client client = (Client) iterator.next();
			if(client.getDocument().equals(document)){
				found = true;
				return client;
			}
		}
		if (!found)
			throw new NotExistDomainException("Cliente");
		return null;
	}

}
